# -*- coding: utf-8 -*-

###### pygs_services.py ##########
from pygs_server import *

from pygs.data import Point
from pygs.interfaces import *
from pygs.services import *
from pygs.persistences import *

### using local service provider
##from pygs.serviceProvider import serviceProviderInstance

from ZSI.fault import Fault, FaultFromException

# using Pyro
import Pyro.core
serviceProviderInstance = Pyro.core.getProxyForURI("PYROLOC://192.168.1.5:7777/ServiceProvider")

def savePoints(ps):
    # save point from request
    pointsToSave = []
    for pointName in ps["points"]:
        pointInfo = ps["points"][pointName]
        pointsToSave.append(
          Point(
            pointInfo["latitude"],
            pointInfo["longitude"],
            pointInfo["altitude"],
            pointInfo["time"],
            pointInfo["pointTypeId"],
            pointInfo["trekingObjectId"]
          )
        )

    token = ps["token"]

    res = serviceProviderInstance.implementation(IInServiceInterface).savePoints(token, pointsToSave)

    response = savePointsSoapOut()
    response._savePointsResult = "PROCEED " + str(ps)
    return response

def login(ps):
    userName = ps["userName"]
    passwd = ps["password"]
    loginParams = {"user": userName, "passwd": passwd}

    token = serviceProviderInstance.implementation(ISecurityInterface).login(loginParams)

    response = loginSoapOut()
    response._loginResult = token
    return response

def logout(ps):
    token = ps["token"]

    result = serviceProviderInstance.implementation(ISecurityInterface).logout(token)

    response = logoutSoapOut()
    response._logoutResult = result
    return response

def getTrek(ps):
    token = ps["token"]
    trekingObjectId = ps["trekingObjectId"]
    dateFrom = ps["dateFrom"]
    dateTo = ps["dateTo"]

    result = serviceProviderInstance.implementation(IOutServiceInterface).getTrek(token, trekingObjectId, dateFrom, dateTo)

    response = getTrekSoapOut()
    response._getTrekResult = result
    return response
